import Promise from 'promise';
import validator from 'validator';
import * as ResultMsg from '../constrants/ResultMsg';
import { errorJson, successJson } from '../common/response';
import { checkId, checkDoc, checkDocs } from '../common/check';
import { MenuProxy, SubmenuProxy } from '../proxy';

export {
  getMenus,
  getSubmenus,
  addMenu,
  deleteMenu,
  updateMenu,
  addSubmenu,
  deleteSubmenu,
  updateSubmenu,
};

async function getMenus(req, res, next) {
  const appId = res.locals.appId;
  const all = req.body.all || false;
  const zoneId = req.body.zoneId || '';
  if (!zoneId) {
    return errorJson(req, res, { err: 'zoneId not found!' });
  }

  const conditions = { zoneId: zoneId, deleted: false };
  const options = { sort: '-weight' };

  if (!all) {
    conditions.enable = true;
  }

  try {
    const docs = await MenuProxy.find(appId, conditions, options);
    successJson(req, res, { data: docs });
  } catch (err) {
    next(err);
  }
}

async function getSubmenus(req, res, next) {
  const appId = res.locals.appId;
  const menuKey = req.body.menuKey;

  if (!menuKey) {
    return errorJson(req, res, { err: 'menu key is empty' });
  }

  try {
    const doc = await MenuProxy.findOne(appId, {key: menuKey });
    const docs = await SubmenuProxy.find(appId, {parent_id: doc._id, deleted: false });
    successJson(req, res, { data: docs });
  } catch (err) {
    next(err);
  }
}

async function updateSubmenu(req, res, next) {
  const appId = res.locals.appId;
  const submenuId = req.params.sid;
  const enable = req.body.enable || false;
  const key = req.body.key || '';
  const value = req.body.value || '';
  const weight = req.body.weight || 0;

  const data = {
    key,
    value,
    enable,
    weight,
  };

  try {
    await SubmenuProxy.findOneById(appId, submenuId);
    await SubmenuProxy.update(appId, submenuId, data);
    successJson(req, res, { msg: ResultMsg.UPDATE_SUCCESS });
  } catch (err) {
    next(err);
  }
}

async function addMenu(req, res, next) {
  const appId = res.locals.appId;
  const key = validator.trim(req.body.key);
  const value = validator.trim(req.body.value);
  const zoneId = validator.trim(req.body.zoneId);

  if (key === '' || value === '' || zoneId === '') {
    return errorJson(req, res, { err: 'key | value | zoneId 参数错误' });
  }

  const conditions = {$or: [{ key }, { value }] };

  try {
    const doc = await MenuProxy.findOne(appId, conditions);
    if (doc) return errorJson(req, res, { err: ResultMsg.REPEAT_DATA });
    const newDoc = await MenuProxy.create(appId, zoneId, key, value);
    successJson(req, res, { data: newDoc });
  } catch (err) {
    next(err);
  }
}

async function deleteMenu(req, res, next) {
  const appId = res.locals.appId;
  const menuId = req.params.mid;

  const data = {
    deleted: false,
  };

  try {
    await checkId(menuId);
    await MenuProxy.update(appId, menuId, data);
    successJson(req, res, { msg: ResultMsg.DELETED_SUCCESS });
  } catch (err) {
    next(err);
  }
}

async function updateMenu(req, res, next) {
  const appId = res.locals.appId;
  const menuId = req.params.mid;
  const enable = req.body.enable || false;
  const key = req.body.key || '';
  const value = req.body.value || '';
  const weight = req.body.weight || 0;

  const data = {
    key,
    value,
    enable,
    weight
  };

  try {
    await checkId(menuId);
    await MenuProxy.update(appId, menuId, data);
    successJson(req, res, { msg: ResultMsg.UPDATE_SUCCESS });
  } catch (err) {
    next(err);
  }
}

async function addSubmenu(req, res, next) {
  const appId = res.locals.appId;
  const parentId = validator.trim(req.body.parentId);
  const key = validator.trim(req.body.key);
  const value = validator.trim(req.body.value);

  if (parentId === '' || key === '' || value === '') {
    return errorJson(req, res, { err: '参数错误' });
  }

  const conditions = { $or: [{ key }, { value }] };

  try {
    const doc = await SubmenuProxy.findOne(appId, conditions);
    if (doc) errorJson(req, res, { err: ResultMsg.REPEAT_DATA });
    const newDoc = SubmenuProxy.create(appId, parentId, key, value);
    successJson(req, res, { data: newDoc });
  } catch (err) {
    next(err);
  }
}

async function deleteSubmenu(req, res, next) {
  const appId = res.locals.appId;
  const submenuId = req.params.sid;

  const data = {
    deleted: true,
  };

  try {
    checkId(submenuId);
    const doc = await SubmenuProxy.findOneById(appId, submenuId);
    if (!doc) errorJson(req, res, { err: ResultMsg.DATA_NOT_FOUND });
    await SubmenuProxy.update(appId, submenuId, data);
    successJson(req, res, { msg: ResultMsg.DELETED_SUCCESS });
  } catch (err) {
    next(err);
  }
}
